package com.easyjava.builder;

import com.easyjava.bean.Constants;
import com.easyjava.bean.FieldInfo;
import com.easyjava.bean.TableInfo;
import com.easyjava.utils.StringUtils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @program: workspace-easyjava
 * @description:
 * @author: cw
 * @create: 2024-05-09 10:57
 **/
public class BuildMapper {

    private static final Logger log = LoggerFactory.getLogger(BuildMapper.class);


    public static void execute(TableInfo tableInfo) {
        File folder = new File(Constants.PATH_MAPPERS);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        OutputStream os = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            String beanName = tableInfo.getBeanName() + Constants.SUFFIX_MAPPER;

            os = new FileOutputStream(folder + "/" + beanName + ".java");
            osw = new OutputStreamWriter(os, "UTF-8");
            bw = new BufferedWriter(osw);

            bw.write("package " + Constants.PACKAGE_MAPPERS + ";\n\n");
            //写入Import
            writeImport(tableInfo, bw);
            //写入类注释
            BuildComment.createClassComment(bw, tableInfo.getComment() + Constants.SUFFIX_MAPPER);
            bw.write("public interface " + beanName + "<T, P> extends BaseMapper {\n\n");

            //写入属性
            writeField(tableInfo, bw);

            bw.write("}");
            bw.flush();
        } catch (Exception e) {
            log.error("创建mapper失败", e);
        } finally {
            BuildBase.close(os, osw, bw);
        }
    }

    private static void writeField(TableInfo tableInfo, BufferedWriter bw) throws Exception {
        Map<String, List<FieldInfo>> keyIndexMap = tableInfo.getKeyIndexMap();
        for (Entry<String, List<FieldInfo>> entry : keyIndexMap.entrySet()) {
            List<FieldInfo> fieldInfoList = entry.getValue();
            Integer index = 0;

            StringBuilder methodName = new StringBuilder();
            StringBuilder methodParam = new StringBuilder();

            for (FieldInfo fieldInfo : fieldInfoList) {
                index++;
                String propertyName = fieldInfo.getPropertyName();
                methodName.append(StringUtils.upperCaseFirst(propertyName));
                if (index < fieldInfoList.size()) {
                    methodName.append("And");
                }

                methodParam.append("@Param(\"" + propertyName + "\") " + fieldInfo.getJavaType() + " " + propertyName);
                if (index < fieldInfoList.size()) {
                    methodParam.append(", ");
                }
            }
            BuildComment.createFieldComment(bw, "根据" + methodName + "查询");
            bw.write("\tT selectBy" + methodName + " (" + methodParam + ");\n\n");

            BuildComment.createFieldComment(bw, "根据" + methodName + "更新");
            bw.write("\tInteger updateBy" + methodName + " (@Param(\"bean\") T t, " + methodParam + ");\n\n");

            BuildComment.createFieldComment(bw, "根据" + methodName + "删除");
            bw.write("\tInteger deleteBy" + methodName + " (" + methodParam + ");\n\n");
        }
    }

    private static void writeImport(TableInfo tableInfo, BufferedWriter bw) throws IOException {
        bw.write("import org.apache.ibatis.annotations.Param;\n\n");
    }

}
